<template>
  <div>
    <el-button type="primary" @click="showAdd = true">添加运动记录</el-button>
    <el-table :data="exerciseList" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="ID" width="60"/>
      <el-table-column prop="userName" label="用户"/>
      <el-table-column prop="content" label="运动内容"/>
      <el-table-column prop="date" label="日期"/>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="editExercise(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteExercise(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="showAdd" title="运动记录">
      <el-form :model="form">
        <el-form-item label="用户"><el-input v-model="form.userName"/></el-form-item>
        <el-form-item label="运动内容"><el-input v-model="form.content"/></el-form-item>
        <el-form-item label="日期"><el-date-picker v-model="form.date" type="date"/></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAdd = false">取消</el-button>
        <el-button type="primary" @click="saveExercise">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { getExerciseListApi, addExerciseApi, updateExerciseApi, deleteExerciseApi } from '../api/exercise'
const exerciseList = ref([])
const showAdd = ref(false)
const form = ref({})
const loadExercises = async () => { exerciseList.value = await getExerciseListApi() }
const saveExercise = async () => {
  if(form.value.id) await updateExerciseApi(form.value)
  else await addExerciseApi(form.value)
  showAdd.value = false
  loadExercises()
}
const editExercise = (row) => { form.value = { ...row }; showAdd.value = true }
const deleteExercise = async (id) => { await deleteExerciseApi(id); loadExercises() }
onMounted(loadExercises)
</script> 